import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.util.HashMap;
import java.util.Map;

public class RedisDemoHash {

    public static void test1(Jedis jedis) {
        System.out.println("jset  hget");
        jedis.flushDB();

        jedis.hset("key","f1","111");
        jedis.hset("key2","f2","222");

        Map<String,String> map = new HashMap<>();
        map.put("f3","333");
        map.put("f4","444");

        jedis.hset("key3",map);
        String field1 = jedis.hget("key", "f1");
        System.out.println("field1 = " + field1);
        String field2 = jedis.hget("key2", "f2");
        System.out.println("field2 = " + field2);
    }

    public static void test2(Jedis jedis) {
        System.out.println("hdel   hexists");
        jedis.flushDB();

        jedis.hset("key","f1","111");
        jedis.hset("key","f2","111");
        jedis.hset("key","f3","111");

        boolean hexists = jedis.hexists("key", "f3");
        System.out.println("hexists = " + hexists);

        long result = jedis.hdel("key", "f3");
        System.out.println("result = " + result);

        hexists = jedis.hexists("key", "f3");
        System.out.println("hexists = " + hexists);
    }

    public static void main(String[] args) {
        JedisPool jedisPool = new JedisPool("tcp://127.0.0.1:8888");
        try (Jedis jedis = jedisPool.getResource()) {
//            test1(jedis);
            test2(jedis);
        }
    }
}
